package cn.share.life.module.images.utils;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UrlAndHtmlUtils {

    static String baseUrl = "https://v33.a505bml.cc/pw/";

    public static void main(String[] args) throws IOException, InterruptedException {
        HtmlFidAndPathEnums[] values = HtmlFidAndPathEnums.values();
        ExecutorService executorService = Executors.newFixedThreadPool(30); // 创建线程池，线程数可以根据需要调整

        for (HtmlFidAndPathEnums enums : values) {
            String webUrl = baseUrl + "thread1022.php?fid=" + enums.getFid() + "&page=";
            String saveDirectory = enums.getBasePath() + enums.getUrlPath();


            for (int i = 1; i <= enums.getPage(); i++) {
                String pageUrl = webUrl + i;
                System.out.println("正在下载网页,----------第 " + i + " 页-------------" + saveDirectory);

                try {
                    ImageFileUtils.disableSSLVerification();
                    Document doc = Jsoup.connect(pageUrl)
                            .userAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MALC)")
                            .get();
                    Elements elements = doc.select("[id^=a_ajax_]");

                    for (Element element : elements) {
                        executorService.submit(() -> {
                            try {
                                String href = element.attr("href");
                                if (StringUtils.isNotEmpty(href)) {
                                    String url = baseUrl + href;
                                    boolean downloaded = downloadAllImagesFromPage(url, saveDirectory);
                                    if (!downloaded) {
                                        System.out.println("网页已存在,跳过下载: " + url);
                                    } else {
                                        System.out.println("网页下载完成: " + url);
                                    }
                                }
                            } catch (Exception e) {
                                System.out.println("下载网页图片出现错误: " + e.getMessage());
                            }
                        });
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        executorService.shutdown(); // 关闭线程池
    }

    public static boolean downloadAllImagesFromPage(String pageUrl, String saveDirectory) {
        try {
            ImageFileUtils.disableSSLVerification();
            Document doc = Jsoup.connect(pageUrl)
                    .userAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MALC)")
                    .get();
            String title = doc.title();
            if (StringUtils.isEmpty(title)) {
                title = "default_" + System.currentTimeMillis();
            }

            Element breadCrumb = doc.getElementById("breadCrumb");
            if (breadCrumb != null) {
                Elements font = breadCrumb.select("font");
                if (font != null && font.size() > 0) {
                    Element last = font.select("a").last();
                    if (last != null) {
                        String html = last.html();
                        if (StringUtils.isNotEmpty(html)) {
                            title = html;
                        }
                    }
                }
            }
            title = title.replaceAll("/", "_");
            saveDirectory = saveDirectory + "/" + title;
            if (Files.exists(Paths.get(saveDirectory))) {
                return false;
            } else {
                System.out.println("创建目录-------------------------------------------------------------: " + saveDirectory);
                Files.createDirectories(Paths.get(saveDirectory));
            }

            System.out.println("正在下载网页: " + title);
            Element readTpc = doc.getElementById("read_tpc");

            String txt = saveDirectory + "/content.txt";
            if (readTpc != null) {
                Files.write(Paths.get(txt), readTpc.html().getBytes(StandardCharsets.UTF_8));
            }

            // 获取网页内所有图片的URL
            Elements imgElements = null;
            if (readTpc != null) {
                imgElements = readTpc.select("img[src]");
            }
            String txt2 = saveDirectory + "/url.txt";
            for (Element imgElement : imgElements) {
                String imgUrl = imgElement.absUrl("src");
                FileUtil.appendLines(Collections.singletonList(imgUrl), txt2, StandardCharsets.UTF_8);
            }
            System.out.println("网页内所有图片下载完成！");
        } catch (Exception e) {
            System.out.println("下载网页图片出现错误: " + e.getMessage());
        }
        return true;
    }
}
